<template>
  <div class="page">
    <!--    <div class="page-search">
      <van-row type="flex" justify="space-between" class="mid">
        <van-col span="18" class="mid">
          <van-search
              class="w100"
              clearable
              v-model="searchData.search"
              shape="round"
              background="#f2f2f2"
              placeholder="请输入"
              @search="onRefresh"
          />
        </van-col>
        <van-col span="6" offset="" class="mid">
          <div class="page-search-option" @click="moduleAction('add')">
            <div class="w100">添加</div>
          </div>
        </van-col>
      </van-row>
    </div>-->
    <div class="page-list">
      <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
        <van-list
          v-model="loading"
          :finished="finished"
          :immediate-check="false"
          :error.sync="errorLoading"
          error-text="请求失败，点击重新加载"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <div
            class="page-list-item"
            v-for="(item, index) in dataList"
            :key="index"
          >
            <div class="page-list-item-inner mid">
              <!-- <div class="edit mid" @click.stop="moduleAction('edit', item)">
                <van-icon
                  class="del-icon"
                  name="cross"
                  color="#ffffff"
                  size="20"
                />
              </div> -->
              <div class="w100">
                <van-row type="flex" justify="start" class="w100">
                  <van-col span="24" class="mid bold">
                    {{ item.name }}
                    <div class="tag" v-if="item.is_student == '1'">学生</div>
                    <div class="tag" v-if="item.relationship_name">
                      {{ item.relationship_name }}
                    </div>
                  </van-col>
                </van-row>
                <div style="margin-top: 10px; display: flex" v-if="item.hsjcsj">
                  <div
                    style="
                      padding: 2px 5px;
                      font-size: 12px;
                      border-radius: 3px;
                      text-align: center;
                      background: rgba(211, 211, 211, 0.5);
                      border: 1px solid grey;
                      color: grey;
                    "
                    v-if="item.hsjcsj"
                  >
                    核酸检测时间:
                    {{ item.hsjcsj }}
                  </div>
                </div>
                <div class="page-list-item-inner-bottom">
                  <van-button
                    class="page-list-item-inner-bottom-stubtn"
                    size="small"
                    color="#46A346"
                    type="info"
                    text="防控填报"
                    v-show="item.is_student == 1"
                    @click="moduleAction('item', item)"
                  />
                  <!-- 去掉是否学生样式判断 -->
                  <van-button
                    class="page-list-item-inner-bottom-stubtn"
                    v-show="isHouseholder || item.card_no == userInfo.card_no"
                    size="small"
                    color="#D58913"
                    type="info"
                    text="修改"
                    @click="moduleAction('edit', item)"
                  />
                  <van-button
                    class="page-list-item-inner-bottom-stubtn"
                    v-if="item.parent_id"
                    v-show="isHouseholder || item.card_no == userInfo.card_no"
                    size="small"
                    color="#D58913"
                    type="info"
                    text="解绑"
                    @click="moduleAction('del', item)"
                  />
                </div>
                <div class="page-list-item-inner-bottoms">
                  <van-button
                    v-show="item.is_student == 1"
                    class="buttonbt page-list-item-inner-bottom-stubtn"
                    size="small"
                    color="#6ca5fb"
                    type="info"
                    text="填报记录及补填"
                    @click="pastRecords('item', item)"
                  />
                </div>
              </div>
              <!-- <van-popover v-model="item.popover" placement="left-end" trigger="click" @select="moduleAction($event.type, item)" :actions="popoverAction">
                <template #reference>
                                <div class="del mid" @click.stop="moduleAction('del', item)">
                  <div class="option mid">
                    <van-icon class="option-icon" name="wap-nav" color="#ffffff" size="20"/>
                  </div>
                </template>
              </van-popover> -->
            </div>
            <!-- <div class="arrow mid" v-show="item.is_student == 1">
              <img :src="url.icon_arrow_right" height="16" width="8" alt="箭头" />
            </div> -->
          </div>
        </van-list>
      </van-pull-refresh>
    </div>

    <div class="bottom-bar">
      <van-button
        class="bottom-bar-btn"
        block
        size="large"
        color="#D58913"
        type="info"
        text="添加家庭成员"
        @click="moduleAction('add')"
      />
    </div>
  </div>
</template>

<script>
import {
  reliefFamily,
  delFamilyInfo,
  delParentsStudent,
  getFamilyList,
  getHSDate,
} from '@/api/school'

export default {
  data() {
    return {
      url: {
        icon_arrow_right: require('../../assets/images/school/icon_arrow_golden.png'),
      },
      route: {},
      page: {
        num: 1,
        total: 0,
      },
      searchData: {},
      loading: true,
      errorLoading: false,
      finished: false,
      refreshing: false,
      dataList: [],
      userInfo: {},
      popoverAction: [
        { type: 'edit', text: '修改', icon: 'edit' },
        { type: 'del', text: '删除', icon: 'cross' },
      ],
      isHouseholder: false,
    }
  },
  computed: {},
  watch: {},
  mounted() {
    this.userInfo = this.$store.state.user.userInfo
    this.route = this.$route.query
    this.init()
  },
  methods: {
    pastRecords(type, item) {
      switch (type) {
        case 'item':
          if (item.is_student == 1) {
            this.$router.push({
              path: '/school/past-records',
              query: {
                parent_id: item.parent_id,
                student_id: item.student_id,
                card_no: item.card_no,
              },
            })
          }
          break
      }
    },
    init() {
      // if (!this.route.parent_id) {
      //   this.$toast('缺少参数')
      //   return
      // }
      this.loading = true
      this.onLoad()
    },
    moduleAction(type, item) {
      switch (type) {
        case 'edit':
          this.$router.push({
            path: '/school/family',
            query: {
              parent_id: item.parent_id,
              id: item.id,
            },
          })
          break
        case 'del':
          this.delAction(item)
          break
        case 'add':
          this.$router.push({
            path: '/school/family',
            query: {
              parent_id: this.route.parent_id,
            },
          })
          break
        case 'item':
          if (item.is_student == 1) {
            this.$router.push({
              path: '/school/healthStudent',
              query: {
                type: 'children',
                parent_id: item.parent_id,
                student_id: item.student_id,
              },
            })
          }
          break
      }
    },
    delAction(item) {
      let that = this
      this.$dialog.confirm({
        title: '是否确认解绑',
        message: '',
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        confirmButtonColor: '#D58913',
        beforeClose: async (action, done) => {
          if (action === 'confirm') {
            const param = { id: item.id }
            // const param = { id: item.id, relationship: item.relationship }
            // const insertData = await delFamilyInfo(param)
            const insertData = await reliefFamily(param)
            if (insertData.data.errcode == 0) {
              this.$toast('操作成功')
              //  this.$router.push({
              //    path: '/school/children-enter',
              //  })
              // if (item.relationship == '1') {
              //   setTimeout(() => {
              //     // this.$router.go(-1)
              that.$router.push({
                path: '/school',
                query: {
                  ...that.$store.state.user.userInfo,
                },
              })
              //   }, 500)
              // } else {
              //   setTimeout(() => {
              //     this.onRefresh()
              //   }, 500)
              // }
              done()
            } else {
              this.$toast(insertData.data.errmsg)
              done()
            }
          } else {
            done()
          }
        },
      })
    },
    getListAction() {
      const data = {
        pageNo: this.page.num,
        pageSize: 20,
        // parent_id: this.route.parent_id,
        search: this.searchData.search,
        card_no: this.userInfo.card_no,
      }
      getFamilyList(data).then((ret) => {
        const data = ret.data
        if (data.errcode === 0) {
          this.dataList.push(...data.data.results)
          this.dataList.forEach(async (item) => {
            // 获取最近核酸时间
            await getHSDate({ idenno: item.card_no }).then((res) => {
              if (res && res.data.errcode === 0) {
                item.hsjcsj = res.data.data.date
              }
            })
            this.$set(item, 'popover', false)
            if (
              item.card_no == this.userInfo.card_no &&
              item.relationship_name == '户主'
            ) {
              this.isHouseholder = true
            }
          })
          this.page.total = data.data.totalRecord
          this.page.num += 1
          if (data.data.pageNo >= data.data.totalPage) {
            this.finished = true
          }
        } else {
          this.errorLoading = true
        }
        this.loading = false
      })
    },
    onRefresh() {
      document.querySelector('html').scrollTop = '0'
      // 清空列表数据
      this.finished = false
      this.refreshing = true
      // 重新加载数据
      this.loading = true
      this.page.num = 1
      this.onLoad()
    },
    onLoad() {
      // 异步更新数据
      // if (this.userInfo.cardNo) {
      if (this.refreshing) {
        this.dataList = []
        this.page.num = 1
        this.refreshing = false
      }
      this.getListAction()
      // } else {
      //   this.loading = false
      // }
    },
  },
}
</script>

<style lang="less" scoped>
.page {
  width: 100%;
  height: 100vh;
  box-sizing: border-box;
  color: black;
  position: relative;
  .page-list {
    height: calc(100% - 50px);
    padding-top: 1.6em;
    .van-pull-refresh {
      height: 100%;
      overflow: hidden auto;
      .van-list {
        height: 100%;
        
      }
    }
  }
  .page-list-item-inner-bottom {
    justify-content: space-between;
    .page-list-item-inner-bottom-stubtn {
      width: 30%;
    }
  }

  .page-list-item-inner-bottoms {
    margin-top: 10px;

    .buttonbt {
      min-width: 110px;
    }
  }

  /deep/.van-search__content--round {
    border-radius: 10px;
  }

  /deep/.van-search__content {
    background-color: #fff;
  }

  &-search {
    width: 100%;

    &-option {
      border-radius: 10px;
      background-color: #fff;
      box-sizing: border-box;
      width: 100%;
      height: auto;
      padding: 5px;
      margin-right: 10px;
      text-align: center;
      font-size: 16px;
      color: #898989;
    }
  }

  &-list {
    box-sizing: border-box;
    width: 100%;
    height: 85vh;

    & > :not(:last-child) {
      border-bottom: 1px solid #ededed;
    }

    &-item {
      margin-bottom: 20px;
      position: relative;

      .arrow {
        position: absolute;
        right: 15px;
        top: 35%;
        width: 40px;
        height: 40px;
      }

      &-inner {
        margin: 0 10px;
        margin-bottom: 15px;
        padding: 20px 40px 20px 30px;
        min-height: 80px;
        text-align: left;
        border-radius: 20px;
        background: #fff;
        position: relative;
        overflow: hidden;

        &-bottom {
          display: flex;
          justify-content: space-around;
          margin-top: 10px;

          &-btn {
            width: 40%;
            border-radius: 5px;
          }

          &-stubtn {
            width: 30%;
            border-radius: 5px;
          }
        }

        .edit {
          position: absolute;
          right: 45px;
          top: 0px;
          background-color: #ff9100;
          width: 40px;
          height: 40px;

          &-icon {
            margin-left: 7px;
            margin-top: 2px;
          }
        }

        .option {
          position: absolute;
          right: -5px;
          top: -5px;
          background-color: #ff9100;
          border-bottom-left-radius: 20px;
          width: 40px;
          height: 40px;

          &-icon {
            margin-left: 7px;
            margin-top: 2px;
          }
        }

        .desc {
          margin-top: 10px;
          font-weight: 400;
          font-size: 20px;
          text-align: left;
          color: #b4b4b4;
        }
      }
    }
  }

  .bottom-bar {
    position: fixed;
    bottom: 0;
    width: 100%;
  }

  .tag {
    margin-left: 10px;
    padding: 2px 5px;
    width: auto;
    height: auto;
    font-size: 12px;
    border-radius: 3px;
    text-align: center;
    background: rgba(211, 211, 211, 0.5);
    border: 1px solid grey;
    color: grey;
  }
}

.NoHouseholder {
  display: flex;
  justify-content: flex-start;
  padding-left: 6px;
}
</style>
